@extends('Admin.layout.layout')

@section('before.css')
    <link href="{{asset('css/plugins/iCheck/custom.css')}}" rel="stylesheet">
@endsection

@section('content')
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>编辑菜单 <small></small></h5>
                    </div>
                    <div class="ibox-content">
                        <form class="form-horizontal m-t" id="Form" action="/foradmin/menu/{{$menu_id}}" method="post">
                            {!!  csrf_field() !!}
                            <input type="hidden" name="_method" value="put">
                            <input type="hidden" name="menu_id" value="{{ $menu_id }}">
                            <div class="modal-body">
                                <div class="form-group">
                                    @inject('menuPresenter','App\Presenters\MenuPresenter')
                                    <label class="col-sm-3 control-label">父级菜单：</label>
                                    <div class="col-sm-8">
                                        <select name="parent_id" class="form-control" id="parent_id">
                                            <option value="0">顶级菜单</option>
                                            @foreach($menuPresenter->levelTree() as $item)

                                                <option @if($parent_id == $item['menu_id'])  selected = "selected" @endif value="{{$item['menu_id']}}">

                                                    {!!  $item['html'] !!}{{ trans($item['name']) }}
                                                </option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">菜单名称：</label>
                                    <div class="col-sm-8">
                                        <input id="name" name="name" value="{{ $name }}" class="form-control" type="text" aria-required="true" aria-invalid="false" class="valid">
                                        @include('Admin.layout.tip',['field' => 'name'])
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">菜单路由：</label>
                                    <div class="col-sm-8">
                                        <input id="route" name="route" value="{{ $route }}" class="form-control" type="text" aria-required="true" aria-invalid="false" class="valid" placeholder="后台路由。例：/foradmin/user">
                                        @include('Admin.layout.tip',['field' => 'route'])
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">接口路径：</label>
                                    <div class="col-sm-8">
                                        <input id="apiroute" name="apiroute" value="{{ $apiroute }}" class="form-control" type="text" aria-required="true" aria-invalid="false" class="valid" placeholder="填写Backend之后路径。例： User\UserController">
                                        @include('Admin.layout.tip',['field' => 'apiroute'])
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">菜单图标：</label>
                                    <div class="col-sm-8">
                                        <input id="icon" name="icon" value="{{ $icon }}" class="form-control" type="text" aria-required="false" aria-invalid="false" class="valid">
                                        @include('Admin.layout.tip',['field' => 'icon'])
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">描述：</label>
                                    <div class="col-sm-8">
                                        <input id="description" value="{{ $description }}" name="description" class="form-control" type="text" aria-required="true" aria-invalid="false" class="valid">
                                        @include('Admin.layout.tip',['field' => 'description'])
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">排序：</label>
                                    <div class="col-sm-8">
                                        <input id="sort" name="sort" value="{{ $sort }}" class="form-control" type="number" aria-required="true" aria-invalid="false" class="valid">
                                        @include('Admin.layout.tip',['field' => 'sort'])
                                    </div>
                                </div>
                                {{--<div class="form-group">--}}
                                    {{--<label class="col-sm-3 control-label">显示：</label>--}}
                                    {{--<div class="col-sm-8">--}}
                                        {{--<div class="radio i-checks">--}}
                                            {{--<label><input @if($hide == 0) checked @endif type="radio" name="hide" value="0">是</label>--}}
                                            {{--<label><input @if($hide == 1) checked @endif type="radio" name="hide" value="1">否</label>--}}
                                        {{--</div>--}}
                                        {{--@include('Admin.layout.tip',['field' => 'hide'])--}}
                                    {{--</div>--}}
                                {{--</div>--}}
                            </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">操作权限：</label>
                                    <input type="hidden" name="operate" >
                                    <div class="col-sm-9">
                                        <div class="radio i-checks">
                                            @if(isset($preAction))

                                            <label>
                                                <input type="checkbox" class="record" data-type="show" name="role[show]" value="{{empty(array_get($preAction,'operate.show.id'))?'on':array_get($preAction,'operate.show.id')}}" {{array_get($preAction,'operate.show.flag')==true?'checked':''}} />查询
                                            </label>
                                            <label>
                                                <input type="checkbox" class="record" data-type="update" name="role[update]"  value="{{empty(array_get($preAction,'operate.update.id'))?'on':array_get($preAction,'operate.update.id')}}"{{array_get($preAction,'operate.update.flag')==true?'checked':''}}/>修改
                                            </label>
                                            <label>
                                                <input type="checkbox" class="record" data-type="destroy" name="role[destroy]"  value="{{empty(array_get($preAction,'operate.destroy.id'))?'on':array_get($preAction,'operate.destroy.id')}}"{{array_get($preAction,'operate.destroy.flag')==true?'checked':''}}/>删除
                                            </label>
                                            <label>
                                                <input type="checkbox" class="record" data-type="store" name="role[store]"  value="{{empty(array_get($preAction,'operate.store.id'))?'on':array_get($preAction,'operate.store.id')}}"{{array_get($preAction,'operate.store.flag')==true?'checked':''}}/>新增
                                            </label>
                                            @else
                                                <label>
                                                <input type="checkbox"  name="role[show]" value="on"  />查询
                                                </label>
                                                <label>
                                                    <input type="checkbox"  name="role[update]"  value="on" />修改
                                                </label>
                                                <label>
                                                    <input type="checkbox"  name="role[destroy]"  value="on" />删除
                                                </label>
                                                <label>
                                                    <input type="checkbox"  name="role[store]"  value="on" />新增
                                                </label>
                                            @endif
                                            <button type="button" class="btn btn-primary btn-sm ml10 qt_btn">其他功能</button>
                                        </div>
                                        <ul id="qt_list">
                                            @if(isset($preAction['elseAction']))
                                            @foreach(array_get($preAction,'elseAction') as $key=>$value)
                                            <li>
                                                <input type="hidden" name="role[{{$key}}][id]"  value="{{$value['id']}}">
                                                <div class="col-sm-3">
                                                    <div class="input-group m-b"><span class="input-group-btn">
                                                        <button type="button" class="btn btn-white">名称</button> </span>
                                                        <input type="text" class="form-control blurCheck" name="role[{{$key}}][name]" value="{{$value['name']}}">
                                                    </div>
                                                </div>
                                                <div class="col-sm-3">
                                                    <div class="input-group m-b"><span class="input-group-btn">
                                                        <button type="button" class="btn btn-white">描述</button> </span>
                                                        <input type="text" class="form-control blurCheck" name="role[{{$key}}][description]" value="{{$value['description']}}">
                                                    </div>
                                                </div>
                                                <div class="col-sm-5">
                                                    <div class="input-group m-b"><span class="input-group-btn">
                                                        <button type="button" class="btn btn-white">控制器</button> </span>
                                                        <input type="text" class="form-control blurCheck"  name="role[{{$key}}][action]" value="{{$value['action']}}">
                                                    </div>
                                                </div>
                                                <div class="col-sm-1">
                                                    <button type="button" data-id="{{$value['id']}}" data-type="{{$value['action']}}" class="btn btn-default btn-sm ml10 del_btn delEvent">删除</button>
                                                </div>
                                            </li>
                                            @endforeach
                                            @endif
                                        </ul>
                                    </div>
                                </div>
                            <div class="modal-footer">
                                <a href="/foradmin/menu" class="pull-left">返回</a>
                                <button type="submit" class="btn btn-primary" id="btn_submit">
                                    确定
                                </button>
                                <a href="/foradmin/menu" class="btn btn-default return"
                                   data-dismiss="modal" >取消
                                </a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="{{asset('js/plugins/iCheck/icheck.min.js')}}"></script>
    <script>
        $(document).ready(function () {
            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green',
                radioClass: 'iradio_square-green',
            });
            var operate = new Array();
            // 记录页面操作，确定到PHP是否增删。
            $('.record').on('ifChecked', function(event){
                var _this = this;
                var splice = _this.value+'-add-'+$(_this).attr("data-type");
                // 是数字被选中的时候是没有修改 后台不做操作
                if($.isNumeric(_this.value)){
                    var delStr = _this.value+'-delete-'+$(_this).attr("data-type");
                    operate.splice($.inArray(delStr,operate),1);
                    
                // 不是数字被选中 增加 store
                }else{
                    operate.push(splice);
                }
                // 赋值给指定的 隐藏域
                $('input[name=operate]').val(operate);
            });
            $('.record').on('ifUnchecked', function(event){
                var _this = this;
                var splice = _this.value+'-delete-'+$(_this).attr("data-type");
                // 是数字被取消 destroy
                if($.isNumeric(_this.value)){
                    operate.push(splice);
                // 不是数字取消 后台不做操作
                }else{
                    var delStr = _this.value+'-add-'+$(_this).attr("data-type");
                    operate.splice($.inArray(delStr,operate),1);
                }
                // 赋值给指定的 隐藏域
                $('input[name=operate]').val(operate);
                
            })
            // 其他功能 输入框 监听修改事件
            $(document).on("blur" , '#route', function(e){
                var val = $('input[name=route]').val();
                console.log(val);
                if (val == '#' || val==''){
                    $('input[name=route]').val('');
                    layer.alert('请按照示例提示输入正确的菜单路由');
                }
                // check 输入的路由是否可存入 menu_permission
                // 即 判断是否已添加过该路由
                $.ajax({
                    url: '/foradmin/menu/checkRoute',
                    type: 'post',
                    dataType: "json",
                    data: {route: val},
                    success: function (data) {
                        console.log(data);
                        if (0 != data.code) {
                            $('input[name=route]').val('');
                            layer.alert(data.message, {
                                icon: 2
                            });
                        }
                    }
                });
            });
            $(document).on("onchange" , '.blurCheck', function(e){
                console.log(this.value);
            });

            $(document).on("click" , '.qt_btn', function(){
                var liNum = $("#qt_list li").length;
                    liNum++;
                var html  = '<li>'+
                                '<div class="col-sm-3">'+
                                    '<div class="input-group m-b"><span class="input-group-btn">'+
                                        '<button type="button" class="btn btn-white">名称</button> </span>'+
                                        '<input type="text" name="role['+liNum+'][name]" class="form-control blurCheck">'+
                                    '</div>'+
                                '</div>'+
                                '<div class="col-sm-3">'+
                                    '<div class="input-group m-b"><span class="input-group-btn">'+
                                        '<button type="button" class="btn btn-white">描述</button> </span>'+
                                        '<input type="text" name="role['+liNum+'][description]" class="form-control blurCheck">'+
                                    '</div>'+
                                '</div>'+
                                '<div class="col-sm-5">'+
                                    '<div class="input-group m-b"><span class="input-group-btn">'+
                                        '<button type="button" class="btn btn-white">控制器</button> </span>'+
                                        '<input type="text" name="role['+liNum+'][action]" class="form-control blurCheck" placeholder=" (UserController@Test) 例：Test" >'+
                                    '</div>'+
                                '</div>'+
                                '<div class="col-sm-1">'+
                                    '<button type="button" data-id="del" data-type="other" class="btn btn-default btn-sm ml10 del_btn">删除</button>'+
                                '</div>'+
                            '</li>';
                $('#qt_list').append(html);
            })
            $(document).on("click" , '.del_btn', function(){
                $(this).parent().parent().remove();
            });
            // 其他功能 删除功能 监听修改事件 // 只监听已有的，新增的不需要监听
            $(document).on("click" , '.delEvent', function(){
                var _this=this;
                var splice = $(_this).attr("data-id")+'-delete-'+$(_this).attr("data-type");

                operate.push(splice);
                // operate.splice($.inArray(splice,operate),1);
                var newVlaue =  $('input[name=operate]').val(operate);

            })

        });
    </script>
@endsection
